package com.sunseagear.wind.common.listener;

import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.sunseagear.common.base.satoken.entity.LoginUser;
import com.sunseagear.common.utils.CacheUtils;
import com.sunseagear.wind.common.utils.UserUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 用户行为 侦听器的实现
 */
@RequiredArgsConstructor
@Component
@Slf4j
public class UserActionListener implements SaTokenListener {

    private final SaTokenConfig tokenConfig;

    /**
     * 每次登录时触发
     */
    @Override
    public void doLogin(String loginType, Object loginId, String tokenValue, SaLoginModel loginModel) {
        LoginUser dto = new LoginUser();
        dto.setToken(tokenValue);
        String username = (String) loginModel.getExtra(UserUtils.USER_NAME_KEY);
        String realname = (String) loginModel.getExtra(UserUtils.REAL_NAME_KEY);
        Long userId = (Long) loginModel.getExtra(UserUtils.USER_ID_KEY);
        dto.setUserId(userId);
        dto.setRealname(realname);
        dto.setUsername(username);
        CacheUtils.valueSet(UserUtils.ONLINE_TOKEN_KEY + tokenValue, dto);
            if(tokenConfig.getTimeout() == -1) {
                CacheUtils.valueSet(UserUtils.ONLINE_TOKEN_KEY + tokenValue, dto);
            } else {
                CacheUtils.valueSet(UserUtils.ONLINE_TOKEN_KEY + tokenValue, dto, tokenConfig.getTimeout(), TimeUnit.SECONDS);
            }
        log.info("user doLogin, userId:{}, token:{}", loginId, tokenValue);
    }

    /**
     * 每次注销时触发
     */
    @Override
    public void doLogout(String loginType, Object loginId, String tokenValue) {

            CacheUtils.valueDelete(UserUtils.ONLINE_TOKEN_KEY + tokenValue);

        log.info("user doLogout, userId:{}, token:{}", loginId, tokenValue);
    }

    /**
     * 每次被踢下线时触发
     */
    @Override
    public void doKickout(String loginType, Object loginId, String tokenValue) {

            CacheUtils.valueDelete(UserUtils.ONLINE_TOKEN_KEY + tokenValue);

        log.info("user doKickout, userId:{}, token:{}", loginId, tokenValue);
    }

    /**
     * 每次被顶下线时触发
     */
    @Override
    public void doReplaced(String loginType, Object loginId, String tokenValue) {
        String tenantId = Convert.toStr(StpUtil.getExtra(tokenValue, UserUtils.TENANT_KEY));

            CacheUtils.valueDelete(UserUtils.ONLINE_TOKEN_KEY + tokenValue);

        log.info("user doReplaced, userId:{}, token:{}", loginId, tokenValue);
    }

    /**
     * 每次被封禁时触发
     */
    @Override
    public void doDisable(String loginType, Object loginId, String service, int level, long disableTime) {
    }

    /**
     * 每次被解封时触发
     */
    @Override
    public void doUntieDisable(String loginType, Object loginId, String service) {
    }

    /**
     * 每次打开二级认证时触发
     */
    @Override
    public void doOpenSafe(String loginType, String tokenValue, String service, long safeTime) {
    }

    /**
     * 每次创建Session时触发
     */
    @Override
    public void doCloseSafe(String loginType, String tokenValue, String service) {
    }

    /**
     * 每次创建Session时触发
     */
    @Override
    public void doCreateSession(String id) {
    }

    /**
     * 每次注销Session时触发
     */
    @Override
    public void doLogoutSession(String id) {
    }

    /**
     * 每次Token续期时触发
     */
    @Override
    public void doRenewTimeout(String tokenValue, Object loginId, long timeout) {
    }
}
